package zdh.avatar.base;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class SoundClip {

    private AudioInputStream audio;
    private Clip clip;
    private String audioPath;

    public SoundClip(String audioPath) {
        this.audioPath = audioPath;
        try {
            clip = AudioSystem.getClip();
        } catch (Exception e) {
            // do something...
        }
        loadClip();
    }

    private void loadClip() {
        try {
            audio = AudioSystem.getAudioInputStream(new File(audioPath));
            clip.open(audio);
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
            // do something...
//            JOptionPane.showMessageDialog(null, e.getMessage());
            System.err.println(e.getMessage());
        }
    }

    public Clip getClip(){
        return clip;
    }
    
    public void play() {
        clip.setFramePosition(0);
        clip.start();
    }
}
